package pcb.views {
	import com.intel.core.Application;
	import com.intel.core.UIComponent;
	import com.intel.utils.Debugger;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import pcb.models.LocaleModel;

	/**
	 * ...
	 * @author Fintan Boyle
	 */
	public class MessagesScreen extends UIComponent implements ILocalise {

		private var context:LoaderContext;
		private var dialogs:MovieClip;
		private var loaded:Boolean = false;
		private var task:String;

		private var legalStr:String = "";
		private var legalTitleStr:String = "";
		private var butAgreeStr:String = "";
		private var butDisagreeStr:String = "";
		private var noIntelStr:String = "";

		private var deferredMessage:Boolean = false;
		private var deferredMessageStr:String;
		
		private var actText:String;
		
		

		public function MessagesScreen(){
			context = new LoaderContext();
			context.applicationDomain = ApplicationDomain.currentDomain;

		}

		override protected function addChildren():void {
			super.addChildren();

		}

		public function showEula(hideBackground:Boolean = true):void {
			if (hideBackground)
				makeBg();

			task = "showEula";
			if (!loaded){
				loadDialogRSL();
			} else {
				drawEula();
			}
		}
		
		public function hideActivityCard():void {
			if (dialogs){
				dialogs.hideActivityCard();
			}
		}
		
		public function showActivityCard(text:String):void {
			task = "showActivityCard";
			
			actText = text;
			
			if (!loaded){
				loadDialogRSL();
			} else {
				drawActivityCard(text);
			}
		}
		
		public function drawActivityCard(text:String):void {
			if (dialogs){				
				//, activityNoteStr
				dialogs.displayActivityCard(text);
			}
		}
		
		private function drawEula():void {

			if (dialogs){
				dialogs.displayEulaDialog(legalTitleStr, legalStr, butAgreeStr, butDisagreeStr);

				var p:Point = Application.getOrigin();
				dialogs.x = (this.stage.stageWidth / 2 - dialogs.width / 2) - p.x;
				dialogs.y = (this.stage.stageHeight / 2 - dialogs.height / 2) - p.y;

			}
		
		}

		public function showMessage(message:String, hideBackground:Boolean = true):void {
			if (hideBackground)
				makeBg();

			task = "showMessage";
			if (!loaded){
				loadDialogRSL();
			} else {
				drawMessage(message);
			}
		}

		public function intelProcessorWarning():void {
			task = "processorWarning";
			if (!loaded){
				loadDialogRSL();
			}
			Debugger.traceOut("intelProcessorWarning");
			drawMessage(noIntelStr);
		}

		private function drawMessage(message:String):void {
			Debugger.traceOut("drawMessage");
			if (dialogs){
				Debugger.traceOut("drawMessage 2" + message);
				dialogs.displayMessage(message);
				Debugger.traceOut("this.stage.fullScreenWidth = " + this.stage.fullScreenWidth);
				Debugger.traceOut("this.stage.fullScreenWidth - stageWidth = " + (this.stage.fullScreenWidth - this.stage.stageWidth));

				var p:Point = Application.getOrigin();
			
				dialogs.x = (this.stage.stageWidth / 2 - dialogs.width / 2) - p.x;
				dialogs.y = (this.stage.stageHeight / 2 - dialogs.height / 2) - p.y;
			} else {
				deferredMessage = true;
				deferredMessageStr = message;
				Debugger.traceOut("Dialog wasn't ready in time to display message: " + message, Debugger.WARNING);
			}
		}

		private function makeBg():void {
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
			this.graphics.endFill();
		}

		//------------------------------------------
		//runtime shared library - dialogs.swf
		//------------------------------------------

		//decided to handle rsl loading in here instead of using Cairngorm Architecture
		private function loadDialogRSL():void {
			var rsl_loader:Loader = new Loader();
			rsl_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, rsl_loader_onComplete);
			rsl_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
			var url:URLRequest = new URLRequest("shared_assets/player/dialogs.swf");
			rsl_loader.load(url, context);
		}

		private function loader_onIOError(e:IOErrorEvent):void {
			Debugger.traceOut("Dialogs rsl IOError");

			e.target.removeEventListener(Event.COMPLETE, rsl_loader_onComplete);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
		}

		private function rsl_loader_onComplete(e:Event):void {
			Debugger.traceOut("Dialogs rsl loaded");
			dialogs = e.currentTarget.content as MovieClip;

			addChild(dialogs);

			loaded = true;
			
			if (task == "showEula"){				
				drawEula();
			} else if (task == "showActivityCard"){				
				showActivityCard(actText);
			} else if (task == "processorWarning"){

				if (deferredMessage){
					drawMessage(deferredMessageStr);
					deferredMessage = false;
				}

			} else {
				drawMessage("");
			}

			task = "";

			e.target.removeEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
			e.target.removeEventListener(Event.COMPLETE, rsl_loader_onComplete);
		}

		/* INTERFACE pcb.views.ILocalise */

		public function localiseLabels(localeModel:LocaleModel):void {			
			legalStr = localeModel.getLabelbyId("eula");
			legalTitleStr = localeModel.getLabelbyId("eulaTitle");
			butAgreeStr = localeModel.getLabelbyId("eulaAgree");
			butDisagreeStr = localeModel.getLabelbyId("eulaDisagree");
			noIntelStr = localeModel.getLabelbyId("noIntel");
		}
	}

}